package cc.forgemod.customicon.client.texture;

import java.util.ArrayList;

import cc.forgemod.customicon.client.ResourceListener;
import cc.forgemod.customicon.common.icon.Icon;
import net.minecraft.client.renderer.block.model.IBakedModel;

public class TextureGifCIItem extends ATextureGifCIItem {

    /** 当前Bake的帧 */
    private IBakedModel mCurrentBackedFrame = null;
    /** 所有Baked的帧 */
    private ArrayList<IBakedModel> mBakedFrame = new ArrayList<>();

    @Override
    public boolean updateIcon(Icon pIcon) {
        super.updateIcon(pIcon);

        this.mBakedFrame.clear();
        int tCount = this.mGif.getFrameCount();
        for (int i = 0; i < tCount; i++) {

            this.initSprite(this.mCacheImage.getWidth(), this.mCacheImage.getHeight(),
                    i * this.width, 0, false);// 更改UV值,用于Frame的Bake
            this.mBakedFrame.add(ResourceListener.bakeTexture(this, false));
        }

        this.moveToFrame(0); //复位
        return true;
    }

    @Override
    public IBakedModel getItemModelWithOverrides(IBakedModel pModel) {
        if (this.mGif == null) return pModel;
        if (System.currentTimeMillis() < this.mNextFrameTime) return this.mCurrentBackedFrame;

        this.moveToFrame(this.mCurrentFrameIndex + 1);
        return this.mCurrentBackedFrame;
    }

    protected void moveToFrame(int pFrame) {
        super.moveToFrame(pFrame);

        this.mCurrentBackedFrame = this.mBakedFrame.get(this.mCurrentFrameIndex);
    }

}
